package com.wb.system.service.impl;

import com.wb.common.utils.DateUtils;
import com.wb.common.utils.SecurityUtils;
import com.wb.common.vo.*;
import com.wb.system.domain.Customer;
import com.wb.system.mapper.CustomerMapper;
import com.wb.system.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 慧尔股份
 *
 * @Description TODO
 * @Author zhangxl
 * @Date 2025/7/19 15:02
 */
@Service
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public List<Customer> customerList(CustomerModelVO customerModelVO) {
        return customerMapper.customerList(customerModelVO);
    }

    @Override
    public int saveCustomer(Customer customer) {
        return customerMapper.saveCustomer(customer);
    }

    @Override
    public int updateCustomer(Customer customer) {
        return customerMapper.updateCustomer(customer);
    }

    @Override
    public Customer getCustomer(Long customerId) {
        return customerMapper.getCustomer(customerId);
    }

    @Override
    public Long todayNewAdditions() {
        long count = customerMapper.todayNewAdditions();
        return count;
    }

    @Override
    public List<SalesmanRankingVO> salesmanRanking() {
        return customerMapper.salesmanRanking();
    }

    @Override
    public ShareStatisticVO shareStatistic() {
        List<String> lastWeekList = DateUtils.getLastWeekDateList(7);
        List<ShareStatisticBarGraphVO> list = customerMapper.shareStatistic(lastWeekList.get(0), lastWeekList.get(lastWeekList.size() - 1));
        return getShareStatisticVO(lastWeekList, list);
    }

    @Override
    public ShareStatisticVO customerConversion() {
        List<String> lastWeekList = DateUtils.getLastWeekDateList(7);
        List<ShareStatisticBarGraphVO> list = customerMapper.customerConversion(lastWeekList.get(0), lastWeekList.get(lastWeekList.size() - 1));
        return getShareStatisticVO(lastWeekList, list);
    }

    private ShareStatisticVO getShareStatisticVO(List<String> lastWeekList, List<ShareStatisticBarGraphVO> list) {
        for (String date : lastWeekList) {
            boolean exist = list.stream().anyMatch(s -> s.getDate().equals(date));
            if (!exist) {
                ShareStatisticBarGraphVO vo = new ShareStatisticBarGraphVO();
                vo.setDate(date);
                vo.setCount(0);
                list.add(vo);
            }
        }
        list.sort(Comparator.comparing(ShareStatisticBarGraphVO::getDate));
        ShareStatisticVO vo = new ShareStatisticVO();
        vo.setXAxis(list.stream().map(ShareStatisticBarGraphVO::getDate).collect(Collectors.toList()));
        vo.setYAxis(list.stream().map(ShareStatisticBarGraphVO::getCount).collect(Collectors.toList()));
        return vo;
    }

    @Override
    public SalesmanStatisticVO salesmanStatistic() {
        Long userId = SecurityUtils.getUserId();
        //总分享次数
        int totalCount = customerMapper.selectCountByShareUserId(userId);
        //已联系的数量
        int contactedCount = customerMapper.selectContactedCountByShareUserId(userId);
        //转化率
        BigDecimal conversionRate = new BigDecimal(0);
        if (totalCount > 0 && contactedCount > 0) {
            conversionRate = new BigDecimal(contactedCount).divide(new BigDecimal(totalCount), 2, RoundingMode.HALF_UP);
        }
        SalesmanStatisticVO salesmanStatisticVO = new SalesmanStatisticVO();
        salesmanStatisticVO.setTotalShareCount(totalCount);
        salesmanStatisticVO.setTotalCustomerCount(totalCount);
        salesmanStatisticVO.setConversionRate(conversionRate);

        String month = DateUtils.getMonthDate();
        String days = String.valueOf(LocalDate.now().lengthOfMonth());
        if (LocalDate.now().lengthOfMonth() < 10) {
            days = "0" + days;
        }
        int monthlyCount = customerMapper.selectMonthlyCountByShareUserId(userId, month, days);
        salesmanStatisticVO.setMonthlyNewCount(monthlyCount);
        return salesmanStatisticVO;
    }
}
